$(function () {
    var ok1 = false;
    var ok2 = false;
    var ok3 = false;
    // var ok4=false;
    // 验证用户名
    var uPattern = /^[-a-zA-Z0-9_\u4E00-\u9FA5]{4,16}$/;
    $('input[name="username"]').focus(function () {
        $(this).next().text('4到16位任意字母数字 _ -汉字').removeClass().addClass('state2');
    }).blur(function () {
        if (uPattern.test($(this).val())) {
            $(this).next().text('输入成功').removeClass().addClass('state4');
            ok1 = true;
        } else {
            $(this).next().text('用户名不符合规范').removeClass().addClass('state3');
            ok1 = false;

        }

    });
    //账号正则
    var mPattern = /^[0-9]{6,15}$/;
    $('input[name="account"]').focus(function () {
        $(this).next().text('请输入正确的6-15位数字账号').removeClass().addClass('state2');
    }).blur(function () {
        if (mPattern.test($(this).val())) {
            let a = $(this).val();
            $(this).next().text('输入成功').removeClass().addClass('state4');
            $.ajax({
                url: "data.json",
                dataType: "json",
                data: {"account": a},
                success: function (data) {
                    console.log(data);
                }
            });
            ok3 = true;
        } else {
            $(this).next().text('账号不符合规范').removeClass().addClass('state3');
            ok3 = false;
        }
    });
    //验证密码
    var pPattern = /^.*(?=.{6,})(?=.*[A-Za-z0-9]).*$/;
    $('input[name="password"]').focus(function () {
        $(this).next().text('可以输入任意大写小写字母数字最少6位').removeClass().addClass('state2');
    }).blur(function () {
        if (pPattern.test($(this).val())) {
            $(this).next().text('输入成功').removeClass().addClass('state4');
            ok2 = true;
        } else {
            $(this).next().text('密码不符合规范').removeClass().addClass('state3');
            ok2 = false;
        }
    });
    //send ajax
    $(".modal-footer button[type='submit']").click(function () {
        if (ok1 && ok2 && ok3) {
            let username = $('input[name="username"]').val();
            let account = $('input[name="account"]').val();
            let identity = $("#edx_admin").val();
            let password = $('input[name="password"]').val();
            console.log(username, account, identity, password);
            $.ajax({
                url: "add_new_worker",
                type: "post",
                dataType: "json",
                data: {"name": username, "account": account, "password": password, "identity": identity},
                success: function (data) {
                    console.log(data);
                }
            });
            $(".edx_mask").fadeIn();
            setTimeout(function () {
                $(".edx_mask").fadeOut();
            }, 1000);
            $("tbody").prepend(arr);
            $('input[name="username"]').val("").next().text("");
            $('input[name="password"]').val("").next().text("");
            $('input[name="account"]').val("").next().text("");
            ok1 = false;
            ok2 = false;
            ok3 = false;
            // j++;
        } else {
            alert("请检查以上填写是否正确");
            return false
        }
    });



    var j;
    for (var b = 2; b > 0; b--) {
        $("<li><a href='javascript:void(0);'>"+b+"</a></li>").insertAfter(".edx_page")
    }
    for (let i = 0; i < 5; i++) {
        $("tbody").append("<tr><td>"+i+"</td><td>"+i+"</td><td>"+i+"</td><td>"+i+"</td></tr>")
    }
//    category page
    let arrli = $(".pagination-lg").children();
    console.log(arrli);
    console.log(b);
    function ajaxPage(page_id) {
            $.ajax({
               url:"personnel_structure",
                data:{"page_id":page_id},
                type:"POST",
                success:function (data) {
                    console.log(data);
                    // {page_num: 2, current_page: "[{"user_id": 17, "name": "电饭锅电饭锅", "identity": "班主…_id": 8, "name": "dfrgfdgfh", "identity": "班主任"}]", page_id: 1}

                }
            });
    };
    ajaxPage(1);
    $.each(arrli, function (i) {
        $(this).on("click", function () {
            if (i === 0 ) {
                if (j===1||j===undefined){
                    j=1;
                    console.log(j);
                    ajaxPage(j);
                }else{
                    console.log(j);
                    j--;
                    console.log(j);
                    ajaxPage(j);
                }
            }else if(i === arrli.length-1){
                if (j===arrli.length-2||j===undefined){
                    j=arrli.length-2;
                    console.log(j);
                    ajaxPage(j);
                }else{
                    j++;
                    console.log(j);
                    ajaxPage(j);
                }
            }
            else{
                    j=i;
                console.log(j);
                ajaxPage(j);
            }
        });
    });
});